package com.os.ops;

import cn.hutool.core.lang.UUID;
import com.os.data.DataHashCache;
import com.os.config.ConfigInit;
import com.os.constant.Sys;
import com.os.exception.Asserts;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
 * @author 风亦未止
 * @date 2023/5/2 17:33
 */
@Log4j2
@Component
public class Obj {
    @Autowired
    private ConfigInit configInit;




    /**
     * put
     *
     * @param hash 对象哈希
     * @return
     */
    public boolean put(String hash, MultipartFile dataSource) {
        String index = dataSource.getOriginalFilename();
        String realPath = configInit.dataPath + Sys.SEPARATOR + hash + "." + index + ".data";
        log.info(realPath);
        File file = new File(realPath);
        FileOutputStream out = null;
        FileLock lock = null;
        try {
            out = new FileOutputStream(file);
            FileChannel channel = out.getChannel();
            lock = channel.lock();
            IOUtils.copy(dataSource.getInputStream(), out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (lock != null) {
                    lock.release();
                }
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        DataHashCache.dataHashCache.put(hash, Integer.parseInt(index));
        return true;
    }


    /**
     * get
     *
     * @param hash 文件哈希
     * @return
     */
    @Deprecated
    public byte[] get(String hash) {
        String realPath = configInit.dataPath + Sys.SEPARATOR + hash + "." + DataHashCache.dataHashCache.get(hash) + ".data";
        log.info(realPath);
        File file = new File(realPath);
        if (!file.exists()) {
            Asserts.fail("文件不存在~");
            return null;
        }
        FileInputStream in = null;
        byte[] bytes = null;
        try {
            in = new FileInputStream(file);
            int size = in.available();
            bytes = new byte[size];
            in.read(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    /**
     * 获取文件流-直接做流传输
     *
     * @param hash
     * @return
     */
    public FileInputStream getReadStream(String hash) {
        String realPath = configInit.dataPath + Sys.SEPARATOR + hash + "." + DataHashCache.dataHashCache.get(hash) + ".data";
        log.info(realPath);
        File file = new File(realPath);
        if (!file.exists()) {
            Asserts.fail("文件不存在~");
            return null;
        }
        FileInputStream read = null;
        try {
            read = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return read;
    }

}
